// Wizard.cpp : implementation file
//

#include "stdafx.h"
#include "Wizard.h"
#include "globals.h"
#include "afxdialogex.h"


// CWizard dialog

IMPLEMENT_DYNAMIC(CWizard, CDialogEx)

CWizard::CWizard(CWnd* pParent /*=NULL*/)
	: CDialogEx(CWizard::IDD, pParent)
{
	m_bdrom =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_BDMOVIE), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_data =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_DATADISC), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_dvdvideo =	(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_DVDMOVIE), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_isowrite =	(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_WRITEISO), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_makeiso =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_HARDISK), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_musiccd =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_MUSICCD), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_photocd =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_PHOTOCD), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_erase =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_ERASE), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);
	m_makeisofromdisc =  (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_MAKEISO), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR);  
	m_musiccddao =  (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_MUSICCD), IMAGE_ICON, 48,48, LR_DEFAULTCOLOR); 
	m_help =		(HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE( IDI_ICON_INFO), IMAGE_ICON, 24,24, LR_DEFAULTCOLOR);
	m_WizardMode = -1;
	EnableWizardOK = FALSE;
	m_FromButton = FALSE;
	scanDlg = NULL;
	 m_visible = FALSE;

}

CWizard::~CWizard()
{
	
}

void CWizard::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON_BDROM, m_Bdrom);
	DDX_Control(pDX, IDC_BUTTON_DATA, m_Data);
	DDX_Control(pDX, IDC_BUTTON_DVDVIDEO, m_Dvdvideo);
	DDX_Control(pDX, IDC_BUTTON_ISOWRITE, m_Isowrite);
	DDX_Control(pDX, IDC_BUTTON_MAKEISO, m_Makeiso);
	DDX_Control(pDX, IDC_BUTTON_MUSICCD, m_Musiccd);
	DDX_Control(pDX, IDC_BUTTON_ERASE, m_Erase);
	DDX_Control(pDX, IDC_BUTTON_PHOTOCD, m_Photo);
	DDX_Control(pDX, IDC_CHECK_RUNWIZARD, m_RunWizard);
	DDX_Control(pDX, IDC_BUTTON_MAKEISOFROMCD, m_MakeIsoFromDisc);
	DDX_Control(pDX, IDC_BUTTON_MUSICCD_GAPLESS, m_MusiccdDAO);
	DDX_Control(pDX, IDC_HELPBUTTON, m_Help);
}


BEGIN_MESSAGE_MAP(CWizard, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_DATA, &CWizard::OnClickedButtonData)
	ON_BN_CLICKED(IDC_BUTTON_BDROM, &CWizard::OnClickedButtonBdrom)
	ON_BN_CLICKED(IDC_BUTTON_DVDVIDEO, &CWizard::OnClickedButtonDvdvideo)
	ON_BN_CLICKED(IDC_BUTTON_ISOWRITE, &CWizard::OnClickedButtonIsowrite)
	ON_BN_CLICKED(IDC_BUTTON_MAKEISO, &CWizard::OnClickedButtonMakeiso)
	ON_BN_CLICKED(IDC_BUTTON_MUSICCD, &CWizard::OnClickedButtonMusiccd)
	ON_BN_CLICKED(IDC_BUTTON_PHOTOCD, &CWizard::OnClickedButtonPhotocd)
	ON_BN_CLICKED(IDC_BUTTON_ERASE, &CWizard::OnClickedButtonErase)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_CHECK_RUNWIZARD, &CWizard::OnClickedCheckRunwizard)
	ON_BN_CLICKED(IDC_BUTTON_MAKEISOFROMCD, &CWizard::OnClickedButtonMakeisofromcd)
	ON_BN_CLICKED(IDC_BUTTON_MUSICCD_GAPLESS, &CWizard::OnClickedButtonMusiccdGapless)
	ON_BN_CLICKED(IDC_HELPBUTTON, &CWizard::OnClickedHelpbutton)
	ON_WM_TIMER()
	ON_WM_WINDOWPOSCHANGING()
END_MESSAGE_MAP()


// CWizard message handlers


BOOL CWizard::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	
	//TaskBar window can be removed by adding WS_EX_TOOLWINDOW to dialog.

	CDialogEx::SetBackgroundColor(GetInt("BackgroundColor", RGB(230,238,235)), TRUE);
	SetFonts();
	SetIcons();
	int wizard = GetInt("RunWizardAtStartup", 1);
	m_RunWizard.SetCheck(wizard);
	if (m_FromButton == FALSE)
	{
		scanDlg = NULL;
		scanDlg = new CScanDrivesMessage(this);
		scanDlg->SetModelessFlag(true);
		scanDlg->Create(IDD_SCAN_DRIVES_DIALOG);
		scanDlg->ShowWindow(SW_SHOW);
		scanDlg->SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
		scanDlg->SetText(" Interrogating your drives, Please Wait...");
		scanDlg->SetText("Initializing Please Wait...");
		EnableUI(FALSE);
		SetTimer(1, 100, NULL);
	}
	else
	{
		m_visible = TRUE;
		ShowWindow(SW_SHOW);
		EnableUI(TRUE);
	}
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CWizard::SetIcons()
{
	m_Bdrom.SetIcon(m_bdrom); 
	m_Data.SetIcon(m_data); 
	m_Dvdvideo.SetIcon(m_dvdvideo); 
	m_Isowrite.SetIcon(m_isowrite); 
	m_Makeiso.SetIcon(m_makeiso); 
	m_Musiccd.SetIcon(m_musiccd); 
	m_Photo.SetIcon(m_photocd); 
	m_Erase.SetIcon(m_erase); 
	m_MakeIsoFromDisc.SetIcon(m_makeisofromdisc);
	m_MusiccdDAO.SetIcon(m_musiccddao);   
	m_Help.SetIcon(m_help); 
}

void CWizard::SetFonts()
{
	LOGFONT lf;  
	// Redraw of child controls is not needed in OnInitDialog since controls 
	// are not drawn yet.
	short int fRedraw= FALSE; 
	memset(&lf, 0, sizeof(LOGFONT));  // Clear out structure.
	lf.lfHeight = 14;                 // Request a 15-pixel-high font
	strcpy_s(lf.lfFaceName, 32, "Microsoft Sans Serif");   // with face name "Arial".
	mFont.CreateFontIndirect(&lf);   // Create the font. 
	SendMessageToDescendants(WM_SETFONT, (WPARAM)mFont.m_hObject,  // Handle to font
							 MAKELONG ((WORD) fRedraw, 0), TRUE);  // Send to all descendants(TRUE) or just children of *this (FALSE)

	GetDlgItem(IDC_BUTTON_BDROM)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_DATA)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_DVDVIDEO)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_ISOWRITE)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_MAKEISO)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_MUSICCD)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_ERASE)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_PHOTOCD)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_MAKEISOFROMCD)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_BUTTON_MUSICCD_GAPLESS)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_HELPBUTTON)->SetFont	(&mFont, TRUE);
	//GetDlgItem(IDC_STATIC_WARNING)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC1)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC2)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC3)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC4)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC5)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC6)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC7)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC8)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATIC9)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_STATICWIZARD)->SetFont	(&mFont, TRUE);
	GetDlgItem(IDC_CHECK_RUNWIZARD)->SetFont	(&mFont, TRUE);
	mFont.Detach();
	CClientDC dc(this);
	VERIFY(mFont.CreatePointFont(80, "Courier", &dc));
	mFont.Detach();
}

void CWizard::EnableUI(BOOL bEnable)
{
	GetDlgItem(IDC_BUTTON_DATA)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_BDROM)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_DVDVIDEO)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_ISOWRITE)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_MAKEISO)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_MUSICCD)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_PHOTOCD)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_ERASE)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_MAKEISOFROMCD)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_MUSICCD_GAPLESS)->EnableWindow(bEnable);
	GetDlgItem(IDCANCEL)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC1)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC2)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC3)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC4)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC5)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC6)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC7)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC8)->EnableWindow(bEnable);
	GetDlgItem(IDC_STATIC9)->EnableWindow(bEnable);
	if (bEnable && scanDlg)
	{
		scanDlg->SetText("");
	}
}

void CWizard::OnClickedButtonData()
{
	m_WizardMode = WIZARD_DATA;
	EndDialog(0);
}


void CWizard::OnClickedButtonBdrom()
{
	m_WizardMode = WIZARD_BDROM;EndDialog(0);
}


void CWizard::OnClickedButtonDvdvideo()
{
	m_WizardMode = WIZARD_DVDVIDEO;
	EndDialog(0);
}


void CWizard::OnClickedButtonIsowrite()
{
	m_WizardMode = WIZARD_ISOWRITE;
	EndDialog(0);
}


void CWizard::OnClickedButtonMakeiso()
{
	m_WizardMode = WIZARD_MAKEISO;
	EndDialog(0);
}


void CWizard::OnClickedButtonMusiccd()
{
	m_WizardMode = WIZARD_MUSICCD;
	EndDialog(0);
}


void CWizard::OnClickedButtonPhotocd()
{
	m_WizardMode = WIZARD_PHOTOCD;
	EndDialog(0);
}


void CWizard::OnClickedButtonErase()
{
	m_WizardMode = WIZARD_ERASE;
	EndDialog(0);
}

void CWizard::OnClose()
{
	m_WizardMode = -1;
	CDialog::OnClose();
}


void CWizard::OnClickedButtonMakeisofromcd()
{
	m_WizardMode = WIZARD_ISOFROMDISC;
	CDialog::EndDialog(0);
}

void CWizard::OnClickedButtonMusiccdGapless()
{
	m_WizardMode = WIZARD_MUSICCDDAO;
	CDialog::EndDialog(0);
}

void CWizard::OnClickedCheckRunwizard()
{
	UpdateData(TRUE);
	WriteInt("RunWizardAtStartup", m_RunWizard.GetCheck());
}



void CWizard::OnClickedHelpbutton()
{
	_TCHAR  sProgramDir[MAX_PATH_UNICODE], drive[_MAX_DRIVE];				
	_TCHAR dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
	_tsplitpath_s((_TCHAR *)AfxGetApp()->m_pszHelpFilePath, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT );
	CString tmp = _T("MagicBurner.chm::/MagicBurner.htm");
	_tmakepath_s(sProgramDir, MAX_PATH_UNICODE, drive, dir, tmp, NULL);
	HWND hwnd = ::HtmlHelp(GetDesktopWindow()->m_hWnd, sProgramDir, HH_DISPLAY_TOPIC, NULL) ;
}


void CWizard::OnTimer(UINT_PTR nIDEvent)
{
	if (EnableWizardOK == TRUE)
	{
		KillTimer(1);
		if (scanDlg)
		{	
			delete scanDlg;
			scanDlg = NULL;
		}
		m_visible = TRUE;
		ShowWindow(SW_SHOW);
		EnableUI(TRUE);
	}
	CDialog::OnTimer(nIDEvent);
}


void CWizard::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
	if(!m_visible)
	{
		lpwndpos->flags &= ~SWP_SHOWWINDOW;
	}
	CDialogEx::OnWindowPosChanging(lpwndpos);

}
